package com.cskaoyan.day21;


public class DownloadTest {
    public static void main(String[] args) {
        DownloadTask task = new DownloadTask();
        new Thread(task, "迅雷下载1").start();
        new Thread(task, "迅雷下载2").start();
        new Thread(task, "迅雷下载3").start();
    }
}

class DownloadTask implements Runnable {

    private int num = 1;
    volatile private boolean flag = true;

    @Override
    public void run() {
        while (flag) {
            System.out.println(Thread.currentThread().getName() + "开始准备执行下载任务");
            down();
            if (flag) {
                System.out.println(Thread.currentThread().getName() + "当前下载任务完成");
            }

        }

    }

    private synchronized void down() {
        if (num <= 100) {
            try {
                Thread.sleep(300);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("\t" + Thread.currentThread().getName() + ": 已经下载" + num + "%");
            num++;
        } else {
            flag = false;
            System.out.println(Thread.currentThread().getName() + ": 资源已经下载完成！");
            return;
        }
    }
}
